package
{
	import com.adobe.serialization.json.JSON;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.FullScreenEvent;
	import flash.events.UncaughtErrorEvent;
	import flash.external.ExternalInterface;
	import flash.media.Camera;
	import flash.media.Microphone;
	import flash.system.Security;
	import flash.ui.Mouse;
	import flash.utils.clearInterval;
	import flash.utils.getTimer;
	import flash.utils.setInterval;
	
	import models.ClassFactoryModel;
	import models.FactoryBase;
	import models.LocalSetting;
	import models.vo.UserVO;
	
	import org.dexter.DexterFramework;
	import org.dexter.sendDexterEvent;
	
	import views.P2PVideo;
	
	import vo.FlashVarsVO;

	[SWF(width="350",height="300")]
	public class WebGospel extends Sprite
	{
		private var background:Shape;
		private const connectUrl:String = "rtmfp://stratus.adobe.com";
		private const developerKey:String = "39eb05e76eabb57deb96b56a-937bc372b0a4";
		[DexterBinding]
		public var stratus:Stratus;
		[DexterBinding]
		public var localSetting:GLocalSetting;
//		[DexterBinding]
//		public var fms:FMS;
		[DexterBinding]
		public var webServer:WebServer;
		[DexterBinding]
		public var appProperty:Object;
		private var main:MainUI = new MainUI();
		private var videos:Array = [];
		private var startIntervalId:uint;
		public function get factory():FactoryBase{
			return DexterFramework.getProperty("classFactory","factory");
		}
		public function WebGospel()
		{
			var flashVars:FlashVarsVO = new FlashVarsVO();
			flashVars.convertFromObject(stage.loaderInfo.parameters);
			main.version.text = "version:"+flashVars.version;
//			loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, onUncaughtError);
			stage.addEventListener(FullScreenEvent.FULL_SCREEN,onFullScreen);
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			videos[1] = main;
			switch(Number(flashVars.videoAmount)){
				case 2:
					videos[2] = new BigUI(2);
					videos[2].y = 300;
					break;
				case 3:
					videos[2] = new SmallUI(2);
					videos[2].y = 302;
					videos[3] = new SmallUI(3);
					videos[3].x = 175;
					videos[3].y = 302;
					break;
				case 4:
					videos[2] = new BigUI(2);
					videos[2].y = 302;
					videos[3] = new SmallUI(3);
					videos[3].y = 584;
					videos[4] = new SmallUI(4);
					videos[4].x = 175;
					videos[4].y = 584;
					break;
				case 5:
					videos[2] = new SmallUI(2);
					videos[2].y = 302;
					videos[3] = new SmallUI(3);
					videos[3].x = 175;
					videos[3].y = 302;
					videos[4] = new SmallUI(4);
					videos[4].y = 454;
					videos[5] = new SmallUI(5);
					videos[5].x = 175;
					videos[5].y = 454;
					break;
			}
			for(var i:int = 1;i<videos.length;i++){
				addChild(videos[i]);
			}
			main.initUI();
			flash.system.Security.allowDomain("tjccn.com");
			flash.system.Security.allowDomain("localhost");
			DexterFramework.register(stage,"stage");
			DexterFramework.register(this,"main");
			DexterFramework.register(flashVars,"flashVars");
			DexterFramework.registerView(main);
			main.setBackground(flashVars.background);
			DexterFramework.register(new GLocalSetting(),"localSetting");
//			DexterFramework.register(new FMS(),"fms");
			DexterFramework.register(new WebServer(flashVars.webServerInterval),"webServer");
			webServer.roomCode = flashVars.roomCode;
			DexterFramework.register(new ClassFactoryModel(),"classFactory");
			DexterFramework.register(new Stratus(),"stratus");
			stratus.groupName = "gospel/"+flashVars.roomCode;
			DexterFramework.register(new ChatController(),"chatCtrl");
			DexterFramework.register(new MessageCtrl(),"msgCtrl");
			DexterFramework.register(new UserListCtrl2(),"userListCtrl");
			DexterFramework.register(new VideoCtrl2(),"videoCtrl");
			DexterFramework.register(new WhiteBoardCtrl(),"wbCtrl");
			sendDexterEvent("setClassFactory",new Factory());
			startIntervalId = setInterval(function():void{
				sendDexterEvent("callJs","appStart");
			},1000);
		}
		private function onUncaughtError(event:UncaughtErrorEvent):void{
			main.version.text = event.text;
		}
		[DexterExternal]
		public function startApp(info:Object):void{
			clearInterval(startIntervalId);
			if(!localSetting.userName)localSetting.userName = "访客"+Math.floor(Math.random()*10000);
			info.name = localSetting.userName;
			info.role = UserVO.GUEST;
			info.sex = localSetting.userSex;
			info.av = (Camera.names.length?'1':'0')+(Microphone.names.length?'1':'0');
			UserVO.self = factory.createUser(info);
//			fms.connect("rtmp://zb.tjccn.com/"+stratus.groupName);
			//stratus.connect(connectUrl+"/"+developerKey);
			//stratus.connect("rtmfp://localhost/multicast");
			stratus.connect("rtmfp://zb.tjccn.com/multicast");
		}
		private function onFullScreen(event:FullScreenEvent):void{
			var p2pVideo:P2PVideo = main.p2pVideo;
			if(event.fullScreen){
				main.removeChild(p2pVideo);
				p2pVideo.x = 0;
				p2pVideo.y = 0;
				addChild(p2pVideo);
				p2pVideo.resize(stage.stageWidth,stage.stageHeight);
				Mouse.hide();
			}else{
				removeChild(p2pVideo);
				p2pVideo.resize(320,240);
				p2pVideo.x = main.videoBack.x;
				p2pVideo.y = main.videoBack.y;
				main.addChild(p2pVideo);
				Mouse.show();
			}
		}
		[DexterEvent]
		public function setVideo(id:String,index:int = 1):void{
			if(UserVO.self.id == id) myVideo = videos[index].p2pVideo;
			else if(videos[index].p2pVideo==myVideo&&id==null)myVideo=null;
			videos[index].setUser(sendDexterEvent("getUserByID",id));
		}
		private var myVideo:P2PVideo;
		[DexterEvent]
		public function changeDevice():void{
			if(myVideo)myVideo.setUser(UserVO.self);
		}
		[DexterEvent]
		public function initAppProperty(appProperty:Object):void{
			DexterFramework.register(appProperty,"appProperty");
			if(appProperty["同步文档"])appProperty["同步文档"]=JSON.decode(appProperty["同步文档"]);
			if(appProperty.music)sendDexterEvent("playMusic",appProperty.music);
			var currentPage:int = appProperty.wb_currentPage?appProperty.wb_currentPage:0;
			if(appProperty.whiteBoard)sendDexterEvent("initWhiteBoard",appProperty.whiteBoard,currentPage);
			else sendDexterEvent("initWhiteBoard",[[]],currentPage);
			sendDexterEvent("callJs","appProperty",appProperty);
		}
		[DexterEvent]
		public function $changeAppProperty(name:String,value:*):void{
			appProperty[name] = value;
			switch(name){
				case "music":
					if(value)sendDexterEvent("playMusic",value);
					else sendDexterEvent("stopMusic");
					break;
			}
			sendDexterEvent("callJs","changeAppProperty",name,value);
		}
	}
}